<template>
  <div>
    <el-row>
      <el-col :span="3">
        <el-card>全部订单({{total}})</el-card>
      </el-col>
      <el-col :span="3">
        <el-card>待付款({{map.s1}})</el-card>
      </el-col>
      <el-col :span="3">
        <el-card>代发货({{map.s2}})</el-card>
      </el-col>
      <el-col :span="3">
        <el-card>已发货({{map.s3}})</el-card>
      </el-col>
      <el-col :span="3">
        <el-card>已完成({{map.s4}})</el-card>
      </el-col>
      <el-col :span="3">
        <el-card>已关闭({{map.s5}})</el-card>
      </el-col>
    </el-row>
    <el-card style="margin-top: 2vh;width: 150vh;text-align: left;height: 10vh">
      <el-input @change="load" size="mini" style="width:180px;margin-right: 15px;" placeholder="请输入订单编号" suffix-icon="el-icon-search" v-model="page.orderNum"/>
      <el-input @change="load" size="mini" style="margin-right: 15px;width:180px;" placeholder="请输入收货人名字、电话" suffix-icon="el-icon-search" v-model="orderNum"/>
      <el-input @change="load" size="mini" style="margin-right: 15px;width:180px;" placeholder="请输入用户ID" suffix-icon="el-icon-search" v-model="page.userId"/>
    </el-card>


      <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg'"
                @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="id" label="序号"></el-table-column>
        <el-table-column prop="orderNum" label="订单编号"></el-table-column>
        <el-table-column prop="userId" label="用户id"></el-table-column>
        <el-table-column prop="commodityName" label="商品名称"></el-table-column>
        <el-table-column prop="commodityNum" label="商品数量"></el-table-column>
        <el-table-column prop="orderMoney" label="订单金额">
          <template slot-scope="scope">
            <span>￥{{scope.row.orderMoney}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="practicalMoney" label="实际消费金额">
          <template slot-scope="scope">
            <span>￥{{scope.row.practicalMoney}}</span>
          </template></el-table-column>
        <el-table-column prop="couponDeduction" label="优惠券抵扣金额">
          <template slot-scope="scope">
            <span>￥{{scope.row.couponDeduction}}</span>
          </template></el-table-column>
        <el-table-column prop="valueCard" label="储值卡抵扣">
          <template slot-scope="scope">
            <span>￥{{scope.row.valueCard}}</span>
          </template></el-table-column>
        <el-table-column prop="returnMoney" label="返现">
          <template slot-scope="scope">
            <span>￥{{scope.row.returnMoney}}</span>
          </template></el-table-column>
        <el-table-column prop="statu" label="状态">
          <template slot-scope="scope">
            <el-tag type="warning" v-if="scope.row.statu==1">待付款</el-tag>
            <el-tag type="success" v-if="scope.row.statu==2">代发货</el-tag>
            <el-tag v-if="scope.row.statu==3">已发货</el-tag>
            <el-tag v-if="scope.row.statu==4">已完成</el-tag>
            <el-tag v-if="scope.row.statu==5">已关闭</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="workUser" label="业务员"></el-table-column>
        <el-table-column prop="commissionAmount" label="提成金额"></el-table-column>
        <el-table-column prop="payType" label="支付方式"></el-table-column>

        <el-table-column prop="createTime" label="下单时间"></el-table-column>

        <el-table-column label="操作" align="center" fixed="right" width="90px">
          <template slot-scope="scope">
            <el-popconfirm
              class="ml-5"

              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="请填写发货单"
              @confirm="handleAdd(scope.row)"
            >
              <el-tag type="success" slot="reference" v-if="scope.row.statu==2">发货<i class="el-icon-remove-outline"></i></el-tag>
            </el-popconfirm>
            <el-tag type="info" v-if="scope.row.statu==1">待付款<i class="el-icon-remove-outline"></i></el-tag>
            <el-tag type="info" v-if="scope.row.statu==3">查看<i class="el-icon-remove-outline"></i></el-tag>
          </template>
        </el-table-column>
      </el-table>

    <div style="padding: 10px 0">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNum"
        :page-sizes="[2, 4, 8, 10]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>

    <!--          添加/修改弹窗-->
    <el-dialog title="下单信息" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form" label-width="80px" size="small">
        <el-form-item label="订单编号">
          <el-input v-model="form.orderNum" disabled autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="快递单号">
          <el-input v-model="form.expressNum" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="业务员">
          <el-input v-model="form.updateUser" disabled autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.updateInfo" autocomplete="off"></el-input>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="changeStatus">确 定</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import {orderList,selectCount,updateStatu} from "../../api/orderList";
import {userList} from "../../api/userCYb";

export default {
  name: "OrderList",
  data(){
    return {
      map:{
      },
      tableData: [],
      users: [],
      orderNum:"",
      multipleSelection: [],
      page:{
        pageNum: 1,
        pageSize: 4,
        orderNum: "",
        userId:""
      },
      total: 0,
      form: {},
      dialogFormVisible: false,
      dialogImageUrl: '',
      dialogVisible: false,
      myHeaders:{'Access-Control-Allow-Origin':'*'}
    }
  },
  created() {
    this.load()
  },
  methods:{
    load() {
      console.log(this.page)
      selectCount().then(res=>{
        this.map=res.data
      })
      orderList(this.page).then(res=>{
        if(res.code==200){
          this.tableData=res.data.records
          this.total = res.data.total
        }else{
          this.$message({
            type:'error',
            message:'请求角色列表失败'
          })
        }
      })

    },
    changeStatus(row) {
      console.log(this.form)
      // if (row.statu==="2"){
        updateStatu(this.form).then(res=>{
          this.$message.success("发货成功")
          this.dialogFormVisible = false
        })
      // }else  if (row.statu==="1"){
      //   this.$message.info("请等待付款")
      // }else {
      //   this.$message.info("已发货")
      // }

      this.load()
    },
    del(id) {
      this.request.delete("/file/" + id).then(res => {
        if (res.code === '200') {
          this.$message.success("删除成功")
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    delBatch() {
      let ids = this.multipleSelection.map(v => v.id)  // [{}, {}, {}] => [1,2,3]
      this.request.post("/file/del/batch", ids).then(res => {
        if (res.code === '200') {
          this.$message.success("批量删除成功")
          this.load()
        } else {
          this.$message.error("批量删除失败")
        }
      })
    },
    reset() {
      this.page.brandName = ""
      this.load()
    },
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.page.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum)
      this.page.pageNum = pageNum
      this.load()
    },
    handleAdd(row){

      let user =JSON.parse(localStorage.getItem("userdata"))
      this.form = row
      this.form.updateUser=user.managerName

      this.dialogFormVisible = true

    },
    save(){
      addBrand(this.form).then(res=>{
        this.$message.success("添加成功")
      })
      console.log(this.form)
    },
    handleAvatarSuccess(res) {
      this.form.logo = res
      this.$message.success("上传成功")
    },

    uploadImg(param){
      const formData = new FormData()
      formData.append('file', param.file)
      // alert(formData)
      this.$axios.post("/management-mall/leyou.mall/mall-goods/test/oss",formData,{
        'Content-type' : 'multipart/form-data'
      }).then(response => {
        this.form.logo="https://"+response.data
      })
    },

    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
  }
}
</script>

<style scoped>
.el-card{
  height: 5vh;
  margin-right:  5vh;
  font-size: 5px;
  text-align: center;
  line-height: 5px;
  color: #E6A23C;
  font-weight: bold;
}
</style>
